package com.efeichong.project.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.efeichong.common.PageData;
import com.efeichong.common.domain.entity.redis.LoginUser;
import com.efeichong.common.exception.CustomException;
import com.efeichong.common.security.LoginContextHolder;
import com.efeichong.common.utils.OptionEmptyFunction;
import com.efeichong.jpa.JExample;
import com.efeichong.project.domain.dao.db.SysPostDao;
import com.efeichong.project.domain.entity.db.SysPost;
import com.efeichong.project.domain.vo.SysPostVo;
import com.efeichong.project.service.SysPostService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * @author
 * @date 2020/12/03
 * @description 岗位信息表
 */
@RequiredArgsConstructor
@Service
public class SysPostServiceImpl implements SysPostService {

    private final SysPostDao sysPostDao;

    /**
     * 通过id查询
     *
     * @param id 主键
     * @return
     */
    @Override
    public SysPostVo getInfo(Long id) {
        return sysPostDao.selectById(id, SysPostVo.class).orElseThrow(OptionEmptyFunction.create());
    }

    /**
     * 分页查询
     *
     * @param sysPostVo
     * @return
     */
    @Override
    public PageData<SysPostVo> selectByPage(SysPostVo sysPostVo) {
        JExample<SysPost> example = new JExample();
        example.initExample(sysPostVo);
        example.orderBy().asc("sort");
        return sysPostDao.selectByPage(example, sysPostVo.getClass());
    }

    @Override
    public List<SysPostVo> selectByList(SysPostVo sysPostVo) {
        JExample<SysPost> example = new JExample();
        example.initExample(sysPostVo);
        example.orderBy().asc("sort");
        return sysPostDao.selectAll(example, sysPostVo.getClass());
    }

    /**
     * 新增
     *
     * @param sysPostVo
     * @return
     */
    @Transactional
    @Override
    public void insertSysPost(SysPostVo sysPostVo) {
        SysPost save = sysPostDao.save(sysPostVo.toPo(SysPost.class));
    }

    /**
     * 修改
     *
     * @param sysPostVo
     * @return
     */
    @Transactional
    @Override
    public void updateSysPost(SysPostVo sysPostVo) {
        if (sysPostVo.getId() == null) {
            throw new CustomException("主键为必传参数");
        }
        sysPostDao.save(sysPostVo.toPo(SysPost.class));
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        sysPostDao.deleteAllByIds(ids);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public void deleteById(Long id) {
        sysPostDao.deleteById(id);
    }

    /**
     * 导入
     *
     * @param inputStream
     * @return
     */
    @Override
    public void importExcel(InputStream inputStream) {
        LoginUser loginUser = LoginContextHolder.getLoginUserOrElseThrow();
        EasyExcel.read()
                .file(inputStream)
                .head(SysPostVo.class)
                .registerReadListener(new ReadListener<SysPostVo>() {
                    @Override
                    public void invoke(SysPostVo sysPostVo, AnalysisContext context) {
                        sysPostDao.save(sysPostVo.toPo(SysPost.class));
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext context) {

                    }
                })
                .doReadAll();
    }

    /**
     * 导出
     *
     * @param sysPostVo
     * @return
     */
    @SneakyThrows
    @Override
    public void exportExcel(SysPostVo sysPostVo, HttpServletResponse response) {
        JExample<SysPost> example = new JExample();
        example.initExample(sysPostVo);
        List<SysPostVo> list = sysPostDao.selectAll(example, SysPostVo.class);
        EasyExcel.write()
                .head(SysPostVo.class)
                .file(response)
                .sheet()
                .doWrite(list);
    }
}
